from django.shortcuts import render, HttpResponse
from django.utils.decorators import method_decorator
from django.http import JsonResponse, HttpResponse
from django.views import View
import json
import os
import datetime
from django.contrib import auth
from django.core import serializers
from django.db.models import Q
from book_manage import models
from listen_book.models import UserInfo
import jieba
# Create your views here.


# 分类获取书籍
class GetBook(View):
    def post(self, request):
        # 参数映射字典
        mapping = {


        }

        pram = request.body
        pram_json = json.loads(pram)
        print(pram_json['type'])

        data = {}
        # 查找书籍
        try:
            result = models.AudioBook.objects.filter(type=pram_json['type'])
            print('success: get type book')
            data['state'] = 'success'
            data['data'] = serializers.serialize('json', result)
        except:
            data['state'] = 'error'
        return JsonResponse(data)


# 排行
class GetRank(View):
    def post(self, request):
        data = {'state': 'success'}

        # 全部
        def all(state):
            result = None

            result1 = models.AudioBook.objects.order_by('-playCount')

            result2 = models.AudioBook.objects.filter(
                pay='免费').order_by('-playCount')

            result3 = models.AudioBook.objects.filter(
                pay='付费').order_by('-playCount')

            result = {
                'all': serializers.serialize('json', result1),
                'free': serializers.serialize('json', result2),
                'vip': serializers.serialize('json', result3)
            }
            return result

        # 男生
        def boy(state):
            result = None

            result1 = models.AudioBook.objects.filter(
                Q(type='都市') | Q(type='玄幻') | Q(type='仙侠武侠') | Q(type='科幻') | Q(type='游戏动漫') | Q(type='悬疑灵异') | Q(type='军事') | Q(type='体育')).order_by('-playCount')

            result2 = models.AudioBook.objects.filter(
                Q(pay='免费') & (Q(type='都市') | Q(type='玄幻') | Q(type='仙侠武侠') | Q(type='科幻') | Q(type='游戏动漫') | Q(type='悬疑灵异') | Q(type='军事') | Q(type='体育'))).order_by('-playCount')

            result3 = models.AudioBook.objects.filter(
                Q(pay='付费') & (Q(type='都市') | Q(type='玄幻') | Q(type='仙侠武侠') | Q(type='科幻') | Q(type='游戏动漫') | Q(type='悬疑灵异') | Q(type='军事') | Q(type='体育'))).order_by('-playCount')

            result = {
                'all': serializers.serialize('json', result1),
                'free': serializers.serialize('json', result2),
                'vip': serializers.serialize('json', result3)
            }
            return result

        # 女生
        def girl(state):
            result = None

            result1 = models.AudioBook.objects.filter(
                Q(type='都市言情') | Q(type='古代言情') | Q(type='仙侠武侠') | Q(type='青春校园') | Q(type='悬疑灵异')).order_by('-playCount')

            result2 = models.AudioBook.objects.filter(
                Q(pay='免费') & (Q(type='都市言情') | Q(type='古代言情') | Q(type='仙侠武侠') | Q(type='青春校园') | Q(type='悬疑灵异'))).order_by('-playCount')

            result3 = models.AudioBook.objects.filter(
                Q(pay='付费') & (Q(type='都市言情') | Q(type='古代言情') | Q(type='仙侠武侠') | Q(type='青春校园') | Q(type='悬疑灵异'))).order_by('-playCount')
            result = {
                'all': serializers.serialize('json', result1),
                'free': serializers.serialize('json', result2),
                'vip': serializers.serialize('json', result3)
            }
            return result

        # 教育

        def edu(state):
            result = None

            result1 = models.AudioBook.objects.filter(
                Q(type='外语') | Q(type='考研') | Q(type='高考') | Q(type='初中') | Q(type='小学') | Q(type='儿童')).order_by('-playCount')

            result2 = models.AudioBook.objects.filter(
                Q(pay='免费') & (Q(type='外语') | Q(type='考研') | Q(type='高考') | Q(type='初中') | Q(type='小学') | Q(type='儿童'))).order_by('-playCount')

            result3 = models.AudioBook.objects.filter(
                Q(pay='付费') & (Q(type='外语') | Q(type='考研') | Q(type='高考') | Q(type='初中') | Q(type='小学') | Q(type='儿童'))).order_by('-playCount')
            result = {
                'all': serializers.serialize('json', result1),
                'free': serializers.serialize('json', result2),
                'vip': serializers.serialize('json', result3)
            }
            return result

        # 休闲娱乐
        def fun(state):
            result = None

            result1 = models.AudioBook.objects.filter(
                Q(type='相声评书') | Q(type='娱乐') | Q(type='戏曲') | Q(type='广播剧') | Q(type='商业财经')).order_by('-playCount')

            result2 = models.AudioBook.objects.filter(
                Q(pay='免费') & (Q(type='相声评书') | Q(type='娱乐') | Q(type='戏曲') | Q(type='广播剧') | Q(type='商业财经'))).order_by('-playCount')

            result3 = models.AudioBook.objects.filter(
                Q(pay='付费') & (Q(type='相声评书') | Q(type='娱乐') | Q(type='戏曲') | Q(type='广播剧') | Q(type='商业财经'))).order_by('-playCount')
            result = {
                'all': serializers.serialize('json', result1),
                'free': serializers.serialize('json', result2),
                'vip': serializers.serialize('json', result3)
            }
            return result

        mapping = {
            'boy': boy,
            'all': all,
            'girl': girl,
            'edu': edu,
            'fun': fun
        }

        # 获取参数
        pram = json.loads(request.body)
        print(pram['type'])
        data['data'] = mapping.get(pram['type'])(pram['state'])
        return JsonResponse(data)


# 推荐
class GetRecommend(View):

    # state 0:喜欢    1：推荐     2：轮播
    def post(self, request):
        # 推荐
        def all(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.all().order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.all().order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.all().order_by('?')[:4]

            return serializers.serialize('json', result)
        # 男生

        def boy(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    Q(type='都市') | Q(type='玄幻') | Q(type='仙侠武侠') | Q(type='科幻') | Q(type='游戏动漫') | Q(type='悬疑灵异') | Q(type='军事') | Q(type='体育')).order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    Q(type='都市') | Q(type='玄幻') | Q(type='仙侠武侠') | Q(type='科幻') | Q(type='游戏动漫') | Q(type='悬疑灵异') | Q(type='军事') | Q(type='体育')).order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.filter(types='boy')

            return serializers.serialize('json', result)
        # 女生

        def girl(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    Q(type='都市言情') | Q(type='古代言情') | Q(type='仙侠武侠') | Q(type='青春校园') | Q(type='悬疑灵异')).order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    Q(type='都市言情') | Q(type='古代言情') | Q(type='仙侠武侠') | Q(type='青春校园') | Q(type='悬疑灵异')).order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.filter(types='girl')

            return serializers.serialize('json', result)
        # 教育

        def edu(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    Q(type='外语') | Q(type='考研') | Q(type='高考') | Q(type='初中') | Q(type='小学') | Q(type='儿童')).order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    Q(type='外语') | Q(type='考研') | Q(type='高考') | Q(type='初中') | Q(type='小学') | Q(type='儿童')).order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.filter(types='edu')

            return serializers.serialize('json', result)
        # 休闲娱乐

        def fun(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    Q(type='相声评书') | Q(type='娱乐') | Q(type='戏曲') | Q(type='广播剧') | Q(type='商业财经')).order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    Q(type='相声评书') | Q(type='娱乐') | Q(type='戏曲') | Q(type='广播剧') | Q(type='商业财经')).order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.filter(types='fun')

            return serializers.serialize('json', result)

        # 免费

        def free(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    pay='免费').order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    pay='免费').order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.all().order_by('?')[:2]

            return serializers.serialize('json', result)

        # 会员

        def vip(state):
            result = None

            if state == 0:
                result = models.AudioBook.objects.filter(
                    pay='付费').order_by('?')[:6]

            elif state == 1:
                result = models.AudioBook.objects.filter(
                    pay='付费').order_by('?')[:3]

            elif state == 2:
                result = models.Swiper.objects.all().order_by('?')[:2]

            return serializers.serialize('json', result)

        def mine(state):
            result = None
            if state == 2:
                result = models.Swiper.objects.all().order_by('?')[:2]

            return serializers.serialize('json', result)

        mapping = {
            'boy': boy,
            'all': all,
            'girl': girl,
            'edu': edu,
            'fun': fun,
            'free': free,
            'vip': vip,
            'mine': mine  # 我的swiper
        }
        data = {'state': 'success'}

        pram = json.loads(request.body)
        print(pram['type'])
        data['data'] = mapping.get(pram['type'])(pram['state'])

        return JsonResponse(data)


# 获取书籍详细以及章节
class GetBookDetiles(View):
    def post(self, request):
        param = json.loads(request.body)
        print(param)
        # 判断用户登录
        if(param['user'] != {}):
            user = UserInfo.objects.filter(username=param['user'])
            print(user)
        else:
            print('未登录')
        books = models.AudioBook.objects.filter(bookId=param['bookId'])
        # 查询前十章节
        topTen = models.Chapter.objects.filter(
            bookId=param['bookId']).order_by('id')[:10]
        res = {
            'book': serializers.serialize('json', books),
            'chapter': serializers.serialize('json', topTen),
        }
        return JsonResponse(res)

    def get(self, request):
        book_id = request.GET.get('id')
        print(book_id)
        result = models.Chapter.objects.filter(bookId=book_id)
        data = {
            'state': 'success',
            'data': serializers.serialize('json', result)
        }
        return JsonResponse(data)


# 搜索
class SearchBook(View):
    def post(self, request):
        param = json.loads(request.body)
        key_code = jieba.cut_for_search(param['keyCode'])
        code = ','.join(key_code).split(',')
        print(code)
        result = []
        # 精确查找
        sr = models.AudioBook.objects.filter(bookName=param['keyCode'])
        result.append(serializers.serialize('json', sr))
        # 模糊查找
        for item in code:
            res = models.AudioBook.objects.filter(bookName__icontains=item)
            result.append(serializers.serialize('json', res))

        data = {
            "state": 200,
            "data": result
        }

        return JsonResponse(data)
